package com.neolix.udpserver;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

public class SliderViewLayout extends FrameLayout {
    private FrameLayout slide;
    private int offsetX = 0;
    private int offsetY = 0;
    public SliderViewLayout(@NonNull Context context) {
        super(context);
        init();
    }

    public SliderViewLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SliderViewLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public SliderViewLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }
    private void init() {
        new Thread(new RunImpl()).start();
    }

    public void initOffset(int x, int y){
        offsetX = x;
        offsetY = y;
        invalidate();
        requestLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        slide = findViewById(R.id.slide);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (slide == null) {
            return;
        }
        int height = getMeasuredHeight();
        int width = getMeasuredWidth();
        int heightSlide = slide.getMeasuredHeight();
        int widthSlide = slide.getMeasuredWidth();
        if (offsetX < 0) {
            offsetX = 0;
        }

        if (offsetX + widthSlide > width)  {
            offsetX = width - widthSlide;
        }

        if (offsetY < 0) {
            offsetY = 0;
        }

        if (offsetY + heightSlide >  height) {
            offsetY = height - heightSlide;
        }
        int l = offsetX ;
        int t = offsetY;
        slide.layout(l, t, l + widthSlide , t + heightSlide);
    }

    public void noticeData(int top, int bottom, int left, int right) {
        if (top == 1) {
            offsetY--;
        } else {
            offsetY++;
        }

        if (bottom == 1) {
            offsetY++;
        } else {
            offsetY--;
        }

        if (left == 1) {
            offsetX--;
        } else {
            offsetX++;
        }

        if (right == 1) {
            offsetX++;
        } else {
            offsetX--;
        }
        requestLayout();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            requestLayout();
        }
    };
    private class RunImpl implements Runnable {

        @Override
        public void run() {
            while(true) {
                try {
                    Thread.sleep(1000);
                    postInvalidate();
                    handler.sendEmptyMessage(0);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
